package com.lenovo.smarttraffic.ui.activity.stage5;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.SensorData;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.util.HttpUtils;
import com.lenovo.smarttraffic.util.Utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

public class RoadFindActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener {

    private Handler handler = new Handler();

    private Toolbar mToolbar;
    private DrawerLayout mDrawer;
    private NavigationView navigationView;

    private TextView road_1, road_2, road_3, road_4, road_7;
    private ImageView road_5, road_6;

    private TextView date_tv, info_tv;
    private ImageView refresh;

    private View male, female;
    private boolean imgFlag = false;

    @Override
    protected int getLayout() {
        return R.layout.activity_road_find;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mToolbar = findViewById(R.id.toolbar_five);
        mDrawer = findViewById(R.id.drawer_layout);
        navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, mDrawer, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        toggle.syncState();
        mDrawer.addDrawerListener(toggle);

        road_1 = findViewById(R.id.road_1);
        road_2 = findViewById(R.id.road_2);
        road_3 = findViewById(R.id.road_3);
        road_4 = findViewById(R.id.road_4);
        road_5 = findViewById(R.id.road_5);
        road_6 = findViewById(R.id.road_6);
        road_7 = findViewById(R.id.road_7);

        date_tv = findViewById(R.id.date_tv);
        info_tv = findViewById(R.id.info_tv);
        refresh = findViewById(R.id.refresh);

        male = findViewById(R.id.male);
        female = findViewById(R.id.female);

        initData();
    }

    private void initData() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd\nEEEE", Locale.CHINA);
        date_tv.setText(dateFormat.format(new Date()));

        refresh.setOnClickListener(v -> HttpUtils.post("get_all_sense", new HttpUtils().setParam("UserName", MainActivity.userName), response -> {
            SensorData sensorData = new Gson().fromJson(response.toString(), SensorData.class);
            info_tv.setText("温度：" + sensorData.getTemperature() + "℃\n相对湿度：" + sensorData.getHumidity() + "%\nPM2.5：" + sensorData.getPM25() + "ug/m3");
        }));

        refreshData.run();
    }

    private Runnable refreshData = new Runnable() {
        @Override
        public void run() {
            male.setBackgroundResource(imgFlag ? R.mipmap.jiaojing1_1 : R.mipmap.jiaojing1_2);
            female.setBackgroundResource(imgFlag ? R.mipmap.jiaojing2_1 : R.mipmap.jiaojing2_2);
            imgFlag = !imgFlag;

            for (int i = 1; i <= 7; i++) {
                int finalI = i;
                HttpUtils.post("get_road_status", new HttpUtils().add("RoadId", i).add("UserName", MainActivity.userName),
                        response -> {
                            Map map = new Gson().fromJson(response.toString(), Map.class);
                            int status = ((Double) map.get("Status")).intValue();
                            int color = R.color.White;
                            if (status == 1) {
                                color = R.color.l1;
                            } else if (status == 2) {
                                color = R.color.l2;
                            } else if (status == 3) {
                                color = R.color.l3;
                            } else if (status == 4) {
                                color = R.color.l4;
                            } else if (status == 5) {
                                color = R.color.l5;
                            }
                            color = Utils.getColor(RoadFindActivity.this, color);

                            if (finalI == 1) {
                                road_1.setBackgroundColor(color);
                            } else if (finalI == 2) {
                                road_2.setBackgroundColor(color);
                            } else if (finalI == 3) {
                                road_3.setBackgroundColor(color);
                            } else if (finalI == 4) {
                                road_4.setBackgroundColor(color);
                            } else if (finalI == 5) {
                                road_5.setImageTintList(ColorStateList.valueOf(color));
                            } else if (finalI == 6) {
                                road_6.setImageTintList(ColorStateList.valueOf(color));
                            } else {
                                road_7.setBackgroundColor(color);
                            }
                        });
            }

            handler.postDelayed(refreshData, 3000);
        }
    };

    @Override
    public void onBackPressedSupport() {
        if (mDrawer.isDrawerOpen(GravityCompat.START)) {
            mDrawer.closeDrawer(GravityCompat.START);
        } else finish();
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        mDrawer.closeDrawer(GravityCompat.START);

        handler.postDelayed(() -> {
            int id = item.getItemId();
            switch (id) {
                case R.id.road_find:
                    startActivity(new Intent(this, RoadFind2Activity.class));
                    break;
                case R.id.road_analyze:
                    startActivity(new Intent(this, RoadAnalyzeActivity.class));
                    break;
                case R.id.bus_find:
                    startActivity(new Intent(this, BusFindActivity.class));
                    break;
                case R.id.bus_find2:
                    startActivity(new Intent(this, BusFind2Activity.class));
                    break;
                case R.id.realtime_bus:
                    startActivity(new Intent(this, RealBusActivity.class));
                    break;
            }
        }, 300);
        return true;
    }
}
